package com.ogawa.chair7808.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.example.reslib.utils.PreferenceWrapper;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.ogawa.base.Constant.CommmandNum;
import com.ogawa.base.utils.CommonUtil;
import com.ogawa.base.utils.StringUtils;
import com.ogawa.chair7808.R;
import com.ogawa.chair7808.bean.Device7808StateBean;
import com.ogawa.chair7808.cache.GlobalCache;
import com.ogawa.chair7808.manager.DataManager;
import com.ogawa.chair7808.ui.iview.IFraAirView7506;
import com.ogawa.chair7808.view.MoveLayout;
import com.ogawa.chair7808.widget.LayoutBody7808;
import com.ogawa.chair7808.widget.LayoutFunctionView7808;
import com.ogawa.chair7808.widget.LayoutGears5_7808;
import com.ogawa.chair7808.widget.ViewTvTime;

import java.util.ArrayList;
import java.util.List;

import static com.ogawa.base.Constant.Constant.DEVICE_7808Core;
import static com.ogawa.base.Constant.MqttConstant.LIVE_EVENT_BUS_KEY_RECEIVE_MESSAGE2;


/**
 * Created by Fhb on 2018/3/29.
 */
public class AirPressureMassageFragment extends BaseTimeTaskFragment implements View.OnClickListener, MoveLayout.OnChooseAction, IFraAirView7506 {

    private View inflate;

    private LinearLayout mLlAirPosition;

    private LayoutBody7808 mLayoutBody7808;
    private LayoutFunctionView7808 mLayoutFunction;

    private ViewTvTime mTvTime;
    private LayoutGears5_7808 mLlAirStrong;

    private ImageView mIvShoulder;
    private ImageView mIvBack;
    private ImageView mIvHand;
    private ImageView mIvLeg;
    private ImageView mIvAll;
    private List<ImageView> mIvAirPosition = new ArrayList<>();
    private String[] name = {"充气位置", "充气强度"};
    private MoveLayout mLayout;
    private ScrollView mScroll;
    private final long INTERVAL = 1000L;
//    private FraAirPresenter7506 mPresenter;
    private long mPauseTime;
    private boolean isPause;
    private ViewStub mView7506;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        inflate = inflater.inflate(R.layout.fragment_air_pressure_massage_7808, container, false);
        initView();
        updateView();


        LiveEventBus.get(LIVE_EVENT_BUS_KEY_RECEIVE_MESSAGE2).observe(this, new androidx.lifecycle.Observer<Object>() {
            @Override
            public void onChanged(Object integer) {
                updateView();

            }
        });

        return inflate;
    }


    private void initView() {
        PreferenceWrapper mSp = new PreferenceWrapper();
        int mChoose = StringUtils.getValue(mSp.getStringValue("app", "1"));
        mIvShoulder = inflate.findViewById(R.id.fragment_air_shoulder);
        mIvBack = inflate.findViewById(R.id.fragment_mt_iv_back);
        if (mChoose == DEVICE_7808Core) {
            mIvBack.setImageResource(R.drawable.selector_gas_waist);
        }
        mIvHand = inflate.findViewById(R.id.fragment_mt_iv_hand);
        mIvLeg = inflate.findViewById(R.id.fragment_mt_iv_leg);
        mIvAll = inflate.findViewById(R.id.fragment_mt_iv_all);
        mScroll = inflate.findViewById(R.id.fragment_air_scoll);
        mLayout = inflate.findViewById(R.id.air_fragment_move_layout);
        mTvTime = inflate.findViewById(R.id.fragment_air_tv_time);
        mLayout.setData(name);
        mLayout.setScollView(mScroll);
        mLayout.setAction(this);
        mIvAirPosition.clear();
        mIvAirPosition.add(mIvShoulder);
        mIvAirPosition.add(mIvBack);
        mIvAirPosition.add(mIvHand);
        mIvAirPosition.add(mIvLeg);
        mIvAirPosition.add(mIvAll);


        mLlAirPosition = inflate.findViewById(R.id.linear_methods_to_select);
        mLlAirStrong = inflate.findViewById(R.id.rl_air_strong);

        mLlAirStrong.setType(4);
//        if (!TextUtils.isEmpty(DataManager.getInst().getValue(CommmandNum.mStatusAirStrong))) {
//            Log.e("setsetstring", DataManager.getInst().getValue(CommmandNum.mStatusAirStrong) + "");
//            mLlAirStrong.setShow(Integer.valueOf(DataManager.getInst().getValue(CommmandNum.mStatusAirStrong)));
//        }
        mView7506 = inflate.findViewById(R.id.layout_body_view);
        if (mLayoutBody7808 == null) {
            mLayoutBody7808 = (LayoutBody7808) mView7506.inflate();
        }
        mLayoutFunction = inflate.findViewById(R.id.fragment_air_layout_function);
//        mPresenter = new FraAirPresenter7506(this);
        mTvTime.setOnClickListener(this);


        mIvShoulder.setOnClickListener(this);
        mIvBack.setOnClickListener(this);
        mIvHand.setOnClickListener(this);
        mIvLeg.setOnClickListener(this);
        mIvAll.setOnClickListener(this);

//        initData();
    }

//    public void initData() {
//        mPresenter.getAirPosition(CommmandNum.mStatusAirShoulder, DataManager.getInst().getValue(CommmandNum.mStatusAirShoulder));
//        mPresenter.getAirPosition(CommmandNum.mStatusAirSit, DataManager.getInst().getValue(CommmandNum.mStatusAirSit));
//        mPresenter.getAirPosition(CommmandNum.mStatusAirHand, DataManager.getInst().getValue(CommmandNum.mStatusAirHand));
//        mPresenter.getAirPosition(CommmandNum.mStatusAirLeg, DataManager.getInst().getValue(CommmandNum.mStatusAirLeg));
//        mPresenter.getAirPosition(CommmandNum.mStatusAirAll, DataManager.getInst().getValue(CommmandNum.mStatusAirAll));
//        mPresenter.getAirPosition(CommmandNum.mStatusAirStrong, DataManager.getInst().getValue(CommmandNum.mStatusAirStrong));
//    }


    private void updateView(){
        Device7808StateBean device7808StateBean = GlobalCache.getDeviceStatusBean();
        mLlAirStrong.setShow(StringUtils.getValue(device7808StateBean.getAirIntensity()));//充气强度
        setPosition(0, StringUtils.getValue(device7808StateBean.getAirCellShoulder())==1);
        setPosition(1, StringUtils.getValue(device7808StateBean.getAirCellHip())==1);
        setPosition(2, StringUtils.getValue(device7808StateBean.getAirCellHand())==1);
        setPosition(3, StringUtils.getValue(device7808StateBean.getAirCellleg())==1);
        setPosition(4, StringUtils.getValue(device7808StateBean.getAirCellWholebody())==1);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
//        mPresenter.onDestory();
        mLayoutFunction.onDestory();
        if (mLayoutBody7808 != null) {
            mLayoutBody7808.onDestory();
        }
//        cancelTime();

    }


    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.fragment_air_tv_time) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {

                showDialog();
            }

        } else if (i == R.id.fragment_air_shoulder) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                DataManager.getDataManager().sendCommond(CommmandNum.AIR_JIANB,"2");

            }

        } else if (i == R.id.fragment_mt_iv_back) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                DataManager.getDataManager().sendCommond(CommmandNum.AIR_BEIB,"2");
            }

        } else if (i == R.id.fragment_mt_iv_hand) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                DataManager.getDataManager().sendCommond(CommmandNum.AIR_SHOUB,"2");
            }

        } else if (i == R.id.fragment_mt_iv_leg) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                DataManager.getDataManager().sendCommond(CommmandNum.AIR_TUIB,"2");
            }

        } else if (i == R.id.fragment_mt_iv_all) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                DataManager.getDataManager().sendCommond(CommmandNum.AIR_ALL,"2");
            }

        }
    }


    @Override
    public void onChoose(int position) {
        mLayout.setTectColor(position);
        switch (position) {
            case 0:
                mLlAirPosition.setVisibility(View.VISIBLE);
                mLlAirStrong.setVisibility(View.INVISIBLE);
                break;
            case 1:
                mLlAirPosition.setVisibility(View.INVISIBLE);
                mLlAirStrong.setVisibility(View.VISIBLE);
                break;
        }

    }

    private void setPosition(int position, boolean isSelect) {
        mIvAirPosition.get(position).setSelected(isSelect);
    }

    @Override
    public void getTime(String time) {
        sendTime(time);
    }

    @Override
    public void getAirPosition(int position, int isRun) {




    }


    public void getAirPosition(String key,String value){
        if(!TextUtils.isEmpty(value)){
            if(key.equals(CommmandNum.mStatusAirShoulder)){
                getAirPosition(0,Integer.valueOf(value));
            }
            else if(key.equals(CommmandNum.mStatusAirSit)){
                getAirPosition(1,Integer.valueOf(value));
            }
            else if(key.equals(CommmandNum.mStatusAirHand)){
                getAirPosition(2,Integer.valueOf(value));
            }
            else if(key.equals(CommmandNum.mStatusAirLeg)){
                getAirPosition(3,Integer.valueOf(value));
            }
            else if(key.equals(CommmandNum.mStatusAirAll)){
                getAirPosition(4,Integer.valueOf(value));
            }
            else if(key.equals(CommmandNum.mStatusAirStrong)){
                getAirStrong(Integer.valueOf(value));
            }
        }

    }


    @Override
    public void getAirStrong(int strong) {
        mLlAirStrong.setShow(strong);
    }


}
